package com.mk.common.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.nutz.dao.Dao;
import org.nutz.dao.util.Daos;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.resource.Scans;

import com.mk.MyNutTestRunner;
import com.mk.common.entity.annotation.TreatCalcAnnotation;
import com.mk.common.model.BizConditionDTO;
import com.mk.common.service.CalcTreatFund;
import com.mk.common.service.impl.LowIncomeCalc;
import com.mk.designMode.strategy.manager.TreatCalcManager;


@RunWith(MyNutTestRunner.class)
@IocBean
public class InterfaceDemo
{
	@Inject
	Dao dao;
	
	@Inject
	Ioc ioc;
	@Inject
	TreatCalcManager treatCalcManager;
	
	
	/**
	 * 默认启动 第一个实现类
	 */
	@Test
	public void test1() {
//		calcTreatFund.calcFund();
		LowIncomeCalc ins =  ioc.get(LowIncomeCalc.class); //√√√
		ins.calcFund(); //√√√
	}
	
	@Test
	public void test2() {
		Daos.createTablesInPackage(dao, "com.mk.car.entity.Car", true);
	}
	
	/**
	 * 配合注解计算
	 */
	@Test
	public void test3() {
		Class<?> clazz = CalcTreatFund.class;
		TreatCalcAnnotation calcAnnotation = clazz.getAnnotation(TreatCalcAnnotation.class);
		System.out.println(calcAnnotation.value()); //java.lang.NullPointerException
		//at com.mk.common.util.InterfaceDemo.test3(InterfaceDemo.java:58)
	}
	
	@Test
	public void test4() {
		List<Class<?>> clazzList = Scans.me().scanPackage("com.mk.common.service.imp");
		System.out.println(".................实现类个数....................");
		System.out.println(clazzList.size());
	}
	
	@Test
	public void test5() {
		List<Class<?>> clazzList = Scans.me().scanPackage("com.mk.common.service.impl");
		int numOfChildClass = clazzList.size();
		//<BizNo, 实现类>
		Map<String,Class<?>> classMap = new HashMap<>(numOfChildClass);
		for(int i=0; i<numOfChildClass; i++) {
			Class<?> clazz = clazzList.get(i);
			//注解
			TreatCalcAnnotation calcAnnotation = clazz.getAnnotation(TreatCalcAnnotation.class);
			if(calcAnnotation==null)
				continue;
			String bizNo = calcAnnotation.value();
			if(bizNo==null || "".equals(bizNo))
				continue;
			//存入
			classMap.put(bizNo, clazz);
		}
		
		
		//
		Class<?> class_ = classMap.get("b004");
//			CalcTreatFund treatFundService = (CalcTreatFund) class_.newInstance();
		CalcTreatFund treatFundService = (CalcTreatFund) ioc.get(class_);
		treatFundService.calcFund();
		
	}
	
	@Test
	public void test6() {
		BizConditionDTO bizCdn = new BizConditionDTO();
//		bizCdn.setBizNo(BasicConstants.BIZ_DSR);
		bizCdn.setBizNo(BasicConstants.BIZ_DB);
//		bizCdn.setBizNo(BasicConstants.BIZ_ZCWY);
//		bizCdn.setBizNo(BasicConstants.BIZ_LSJJ);
		treatCalcManager.calcTreat(bizCdn);
	}
	
}
